package com.baboba.rummy.web.client;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import com.baboba.rummy.card.CardBitMap;
import com.baboba.rummy.card.CardComparator;
import com.baboba.rummy.card.CardFactory;
import com.baboba.rummy.card.Color;
import com.baboba.rummy.card.UserCard;
import com.baboba.rummy.robot.IceBreaker;
import com.baboba.rummy.web.client.card.CardListView;
import com.baboba.rummy.web.client.card.IceBreakView;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class IceBreak implements EntryPoint {

	private Button randomButton = new Button("Random");
	private FlexTable cardsFlexTable = new FlexTable();
	private IceBreakView iceBreakView = new IceBreakView();

	@Override
	public void onModuleLoad() {
		randomButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				LinkedList<UserCard> allCards = CardFactory.generateRandomShownCardsAsList();
				final LinkedList<UserCard> currentCards = new LinkedList<UserCard>();

				for (int i = 1; i <= 14; i++) {
					currentCards.add(allCards.getFirst());
					allCards.removeFirst();
				}

				renderCardsTables(currentCards);
			}

		});

		List<LinkedList<UserCard>> cards = getTestCards();
		for (LinkedList<UserCard> cardList : cards)
			renderCardsTables(cardList);

		VerticalPanel topContainer = new VerticalPanel();
		topContainer.add(randomButton);
		topContainer.add(iceBreakView);
		topContainer.add(cardsFlexTable);
		RootLayoutPanel.get().add(topContainer);
	}

	private void renderCardsTables(final LinkedList<UserCard> currentCards) {
		Collections.sort(currentCards, new CardComparator());
		CardListView cardListView = new CardListView(currentCards);

		cardsFlexTable.insertRow(0);
		cardsFlexTable.setWidget(0, 0, cardListView);

		Button button = new Button("Ice Break");
		button.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				iceBreakView.showIceBreak(IceBreaker.awkwardBreak(new CardBitMap(currentCards)));
			}

		});

		cardsFlexTable.setWidget(0, 1, button);
	}

	public List<LinkedList<UserCard>> getTestCards() {
		List<LinkedList<UserCard>> result = new LinkedList<LinkedList<UserCard>>();

		LinkedList<UserCard> cards = new LinkedList<UserCard>();
		cards.add(new UserCard(13, Color.Black));
		cards.add(new UserCard(13, Color.Blue));
		cards.add(new UserCard(13, Color.Red));
		cards.add(new UserCard(13, Color.Orange));
		cards.add(new UserCard(13, Color.Blue));
		cards.add(new UserCard(13, Color.Red));
		cards.add(new UserCard(13, Color.Orange));
		cards.add(new UserCard(1, Color.Orange));
		cards.add(new UserCard(3, Color.Orange));
		cards.add(new UserCard(5, Color.Orange));
		cards.add(new UserCard(7, Color.Orange));
		cards.add(new UserCard(9, Color.Orange));
		cards.add(new UserCard(11, Color.Orange));
		cards.add(new UserCard(2, Color.Red));
		result.add(cards);

		cards = new LinkedList<UserCard>();
		cards.add(new UserCard(13, Color.Red));
		cards.add(new UserCard(12, Color.Red));
		cards.add(new UserCard(11, Color.Red));
		cards.add(new UserCard(10, Color.Red));
		cards.add(new UserCard(9, Color.Red));
		cards.add(new UserCard(1, Color.Orange));
		cards.add(new UserCard(3, Color.Orange));
		cards.add(new UserCard(5, Color.Orange));
		cards.add(new UserCard(7, Color.Orange));
		cards.add(new UserCard(9, Color.Orange));
		cards.add(new UserCard(11, Color.Orange));
		cards.add(new UserCard(2, Color.Red));
		cards.add(new UserCard(2, Color.Black));
		cards.add(new UserCard(2, Color.Blue));

		result.add(cards);

		return result;
	}
}
